package cn.sumpu.app.guoguojie.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.TopShopBiz;
import cn.sumpu.app.guoguojie.constant.Channel;
import cn.sumpu.app.guoguojie.dao.TopShopDao;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.domain.TaobaokeShop;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

public class TopShopBizImpl implements TopShopBiz {
    private TopShopDao topShopDao;

    public void setTopShopDao(TopShopDao topShopDao) {
        this.topShopDao = topShopDao;
    }

    @Autowired
    private CmsContentBiz contentBiz;

    @Override
    public void saveTopShop(TaobaokeShop taobaokeShop, String saveType) throws AppBusinessException {
        if (!Common.empty(taobaokeShop)) {
            if (saveType.equals("add")) {
                topShopDao.insertTopShop(taobaokeShop);
            } else if (saveType.equals("edit")) {
                topShopDao.updateTopShop(taobaokeShop);
            }
        }
    }

    @Override
    public void updateTopShopBySellerId(String sellerId) throws AppBusinessException {
        TaobaokeShop taobaokeShop = new TaobaokeShop();
        taobaokeShop.setSellerId(sellerId);
        topShopDao.updateTopShop(taobaokeShop);
    }

    @Override
    public void deleteTopShopById(String id) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", id);
        topShopDao.deleteTopShop(map);
    }

    @Override
    public TaobaokeShop getTopShopById(String id) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", id);
        map.put("proSize", 0);
        List<TaobaokeShop> list = topShopDao.getTopShop(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public TaobaokeShop getTopShopBySellerId(String sellerId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sellerId", sellerId);
        map.put("proSize", 0);
        List<TaobaokeShop> list = topShopDao.getTopShop(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageModel getTopShop(Integer page, Integer pageSize, Integer proSize, String shopName) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        page = page == null ? 1 : page;
        int size = pageSize == null ? 10 : pageSize;
        map.put("startId", (page - 1) * size);
        map.put("pageSize", size);
        if (!Common.empty(shopName)) {
            map.put("shopName", "%" + shopName + "%");
        }
        map.put("proSize", proSize);

        int count = topShopDao.getTopShopCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<TaobaokeShop> list = topShopDao.getTopShop(map);
        pm.setList(list);
        pm.setPageSize(size);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + size - 1) / size);
        return pm;
    }

    @Override
    public List<TaobaokeShop> getIndexShop() throws AppBusinessException {
        List<CmsContent> list = contentBiz.getContentList(Channel.INDEX_SHOP, 4);
        List<TaobaokeShop> shopList = new ArrayList<TaobaokeShop>();
        if (list != null && list.size() > 0) {
            for (CmsContent cmsContent : list) {
                String sellerId = cmsContent.getTitle();
                TaobaokeShop shop = getTopShopBySellerId(sellerId);
                if (!Common.empty(shop)) {
                    shop.setDescription(cmsContent.getDescription());
                    shopList.add(shop);
                }
            }
        }
        return shopList;
    }
}
